package io.github.wslxm.springbootplus2.manage.gc.generate.util;

import io.github.wslxm.springbootplus2.manage.gc.config.GcConfig;
import io.github.wslxm.springbootplus2.manage.gc.constant.BracketConstant;
import io.github.wslxm.springbootplus2.manage.gc.constant.FieldTypeConstant;
import io.github.wslxm.springbootplus2.manage.gc.utils.GcDataUtil;

/**
 * 生成服务器代码工具
 *
 * @author wangsong
 * @date 2023/08/28
 */
public class GenerateServerUtil {


    private final static String NO = "NO";

    /**
     * 数据库类型对应字段生成
     * <p>
     * 用于 entity+dto+vo+query
     * </P>
     *
     * @param gcConfig  配置信息
     * @param fieldName 字段名
     * @param type      字段类型
     * @param isTimeStr 时间是否使用字符串,只处理 datetime 字段类型（特殊处理, 主要处理满足 query 范围查询的代码自动生成）
     */
    public static String jxModel(GcConfig gcConfig, String fieldName, String type, Boolean isTimeStr) {

        isTimeStr = isTimeStr != null && isTimeStr;
        // 转驼峰模式
        fieldName = GcDataUtil.getFieldName(gcConfig, fieldName);
        String field = "";
        // 字段
        if (type.equals(FieldTypeConstant.MEDIUMINT) || type.equals(FieldTypeConstant.SMALLINT) || type.equals(FieldTypeConstant.INT) || type.equals(FieldTypeConstant.TINYINT)) {
            // 整数int
            field = "private Integer " + fieldName + ";";
        } else if (type.equals(FieldTypeConstant.BIGINT)) {
            // 整数Long
            field = "private Long " + fieldName + ";";
        } else if (type.equals(FieldTypeConstant.VARCHAR) || type.equals(FieldTypeConstant.CHAR)) {
            // 字符串
            field = "private String " + fieldName + ";";
        } else if (type.equals(FieldTypeConstant.BINARY) || type.equals(FieldTypeConstant.VARBINARY)) {
            // 字符串
            field = "private String " + fieldName + ";";
        } else if (type.equals(FieldTypeConstant.TEXT) || type.equals(FieldTypeConstant.LONG_TEXT) || type.equals(FieldTypeConstant.JSON)) {
            // 大文本、超大文本、json
            field = "private String " + fieldName + ";";
        } else if (type.equals(FieldTypeConstant.DATETIME) || type.equals(FieldTypeConstant.TIME)
                || type.equals(FieldTypeConstant.TIMESTAMP) || type.equals(FieldTypeConstant.DATE)) {
            // 时间
            if ((type.equals(FieldTypeConstant.DATETIME) || type.equals(FieldTypeConstant.DATE))
                    && isTimeStr) {
                field = "private String " + fieldName + ";";
            } else {
                field = "private LocalDateTime " + fieldName + ";";
            }
        } else if (type.equals(FieldTypeConstant.DOUBLE)) {
            // 双精度小数 Double
            field = "private Double " + fieldName + ";";
        } else if (type.equals(FieldTypeConstant.FLOAT)) {
            // 单精度小数 Float
            field = "private Float " + fieldName + ";";
        } else if (type.equals(FieldTypeConstant.DECIMAL)) {
            // 小数 decimal
            field = "private BigDecimal " + fieldName + ";";
        }
        //   else if (type.equals(FieldTypeConstant.TINYINT)) {
        //       // 布尔 tinyint
        //       field = "private Boolean " + fieldName + ";";
        //   }
        return field;
    }


    /**
     * 必填字段获取 jsr303 验证注解，
     *
     * @param isNull     NO 代表必填, YES 非必填
     * @param type       字段类型
     * @param typeDetail 字段类型长度,  如: int(11) 在 mysql8.0.16+ 版本后, 删除了 int /bigint 的长度支持 (自动使用最大值)
     * @param desc       字段备注
     * @param isRequired 是否判断必填 true 是 false 否, query 查询中不需要验证必传
     */
    public static String jsrModel(String isNull, String type, String typeDetail, String desc, boolean isRequired) {
        String jsr = "";
        if ((NO).equals(isNull) && isRequired) {
            if (type.equals(FieldTypeConstant.VARCHAR) || type.equals(FieldTypeConstant.CHAR)
                    || type.equals(FieldTypeConstant.TEXT) || type.equals(FieldTypeConstant.LONG_TEXT)
                    || type.equals(FieldTypeConstant.BINARY) || type.equals(FieldTypeConstant.VARBINARY)
            ) {
                jsr = "    @NotBlank(message = \"{DESC} 不能为空\")";
            } else {
                jsr = "    @NotNull(message = \"{DESC} 不能为空\")";
            }
            jsr = jsr.replace("\\{DESC}", desc);
            jsr += "\n";
        }
        // 获取数据库注释,去除括号后的内容
        desc = desc.contains(BracketConstant.LEFT_BRACKET) ? desc.substring(0, desc.indexOf(BracketConstant.LEFT_BRACKET)) : desc;
        desc = desc.contains(BracketConstant.LEFT_BRACKET_TWO) ? desc.substring(0, desc.indexOf(BracketConstant.LEFT_BRACKET_TWO)) : desc;
        desc = desc.contains(BracketConstant.LEFT_BRACKET_THREE) ? desc.substring(0, desc.indexOf(BracketConstant.LEFT_BRACKET_THREE)) : desc;
        desc = desc.contains(BracketConstant.LEFT_BRACKET_FOUR) ? desc.substring(0, desc.indexOf(BracketConstant.LEFT_BRACKET_FOUR)) : desc;
        desc = desc.contains(BracketConstant.LEFT_BRACKET_FIVE) ? desc.substring(0, desc.indexOf(BracketConstant.LEFT_BRACKET_FIVE)) : desc;
        desc = desc.contains(BracketConstant.LEFT_BRACKET_SIX) ? desc.substring(0, desc.indexOf(BracketConstant.LEFT_BRACKET_SIX)) : desc;

        //字段
        String maxlength = "";
        if (type.equals(FieldTypeConstant.INT) || type.equals(FieldTypeConstant.BIGINT) || type.equals(FieldTypeConstant.TINYINT)
                || type.equals(FieldTypeConstant.SMALLINT) || type.equals(FieldTypeConstant.MEDIUMINT)
        ) {
            // 整数
            // int(11), 判断是否有长度,存在长度获取指定长度的最大值, 转为long添加到注解中
            if (typeDetail.contains(BracketConstant.LEFT_BRACKET)) {
                int len = Integer.parseInt(typeDetail.substring(typeDetail.indexOf("(") + 1, typeDetail.indexOf(")")));
                maxlength = len >= 19 ? Long.MAX_VALUE + "" : GenerateUtil.numberGenerator(9, len, 0);
            } else {
                maxlength = GenerateUtil.getDefaultMaxlength(type);
            }
            jsr += "    @Range(min=0L, max={MAX}L,message = \"{DESC} 必须>=0 和 <={MAX}\")";
            jsr = jsr.replaceAll("\\{MAX}", maxlength + "").replace("{DESC}", desc);
        } else if (type.equals(FieldTypeConstant.DOUBLE) || type.equals(FieldTypeConstant.FLOAT) || type.equals(FieldTypeConstant.DECIMAL)) {
            // 小数 判断是否有长度,存在长度获取指定长度的最大值, 转为long添加到注解中 decimal(10,2)，取10, 2不处理
            if (typeDetail.contains(BracketConstant.LEFT_BRACKET)) {
                String typeDetailStr = typeDetail.substring(typeDetail.indexOf(BracketConstant.LEFT_BRACKET) + 1, typeDetail.indexOf(BracketConstant.RIGHT_BRACKET));
                String[] typeDetailStrArray = typeDetailStr.split(",");
                if (typeDetailStrArray.length > 1) {
                    maxlength = GenerateUtil.numberGenerator(9, Integer.parseInt(typeDetailStrArray[0]), Integer.parseInt(typeDetailStrArray[1]));
                } else {
                    maxlength = GenerateUtil.numberGenerator(9, Integer.parseInt(typeDetailStrArray[0]), 0);
                }
            } else {
                maxlength = GenerateUtil.getDefaultMaxlength(type);
            }
            jsr += "    @DecimalMin(value=\"0\",message=\"{DESC} 必须 >= 0\")";
            jsr += "\n    @DecimalMax(value=\"{MAX}\",message=\"{DESC} 必须 <= {MAX}\")";
            jsr = jsr.replaceAll("\\{MAX}", maxlength).replace("{DESC}", desc);
        } else if (type.equals(FieldTypeConstant.VARCHAR) || type.equals(FieldTypeConstant.CHAR) || type.equals(FieldTypeConstant.TEXT) || type.equals(FieldTypeConstant.LONG_TEXT)) {
            // 字符串
            if (typeDetail.contains(BracketConstant.LEFT_BRACKET)) {
                maxlength = typeDetail.substring(typeDetail.indexOf(BracketConstant.LEFT_BRACKET) + 1, typeDetail.indexOf(BracketConstant.RIGHT_BRACKET));
            } else {
                maxlength = GenerateUtil.getDefaultMaxlength(type);
            }
            jsr += "    @Length(min=0, max={MAX},message = \"{DESC} 必须>=0 和 <={MAX}位\")";
            jsr = jsr.replaceAll("\\{MAX}", maxlength).replace("{DESC}", desc);
        } else if (type.equals(FieldTypeConstant.DATETIME) || type.equals(FieldTypeConstant.TIME) || type.equals(FieldTypeConstant.TIMESTAMP)) {
            // 时间暂无
        }
        return jsr;
    }


}
